from typing import List, Optional
from game.card import Card
from game.skill import Skill

# 角色身份常量
ROLE_LORD = "主公"
ROLE_LOYAL = "忠臣"
ROLE_REBEL = "反贼"
ROLE_TRAITOR = "内奸"

class Character:
    """角色类，代表游戏中的玩家"""
    def __init__(self, name: str, faction: str, max_health: int, is_human: bool = False):
        self.name = name
        self.faction = faction  # 魏、蜀、吴、群
        self.max_health = max_health
        self.health = max_health
        self.is_human = is_human
        self.role = None  # 主公、忠臣、反贼、内奸
        self.is_lord = False
        self.dead = False
        
        # 牌区
        self.hand: List[Card] = []  # 手牌
        self.equipment_area: List[Card] = []  # 装备区
        self.judge_area: List[Card] = []  # 判定区
        
        # 技能列表
        self.skills: List[Skill] = []
        
        # 回合状态
        self.attack_count = 0  # 已使用杀的次数
        self.extra_attack_count = 0  # 额外杀次数
        self.has_drunk = False  # 是否处于醉酒状态
    
    def add_skill(self, skill: Skill):
        """添加技能"""
        self.skills.append(skill)
    
    def remove_skill(self, skill_name: str):
        """移除技能"""
        self.skills = [s for s in self.skills if s.name != skill_name]
    
    def has_skill(self, skill_name: str) -> bool:
        """检查是否拥有指定技能"""
        return any(skill.name == skill_name for skill in self.skills)
    
    def take_damage(self, damage: int, source=None, is_fire: bool = False, is_thunder: bool = False):
        """受到伤害"""
        # 检查是否有技能可以响应伤害
        for skill in self.skills:
            if hasattr(skill, 'on_take_damage'):
                damage = skill.on_take_damage(self, damage, source, is_fire, is_thunder)
        
        self.health -= damage
        if self.health <= 0:
            self.die(source)
    
    def heal(self, value: int):
        """回复体力"""
        self.health = min(self.health + value, self.max_health)
    
    def die(self, source=None):
        """死亡"""
        self.dead = True
        self.health = 0
        
        # 处理死亡时的身份结算
        if source:
            self.resolve_death(source)
    
    def resolve_death(self, source):
        """处理死亡时的身份结算"""
        # 根据身份判断奖惩
        if self.role == ROLE_REBEL and source.role == ROLE_LORD:
            # 主公杀死反贼，摸三张牌
            pass  # 这里会在GameEngine中实现
        elif self.role == ROLE_LOYAL and source.role == ROLE_LORD:
            # 主公误杀忠臣，弃掉所有牌
            pass  # 这里会在GameEngine中实现
    
    def get_attack_range(self) -> int:
        """获取攻击距离"""
        range_value = 1
        
        # 检查武器
        for equip in self.equipment_area:
            if hasattr(equip, 'attack_range'):
                range_value = equip.attack_range
                break
        
        # 检查进攻马
        for equip in self.equipment_area:
            if hasattr(equip, 'is_attack_horse') and equip.is_attack_horse:
                range_value += 1
                break
        
        return range_value
    
    def calculate_distance(self, target) -> int:
        """计算与目标的距离"""
        # 这个方法需要在GameEngine中实现，因为需要知道所有玩家的位置
        return 1
    
    def has_armor(self) -> bool:
        """检查是否有防具"""
        return any(hasattr(equip, 'is_armor') and equip.is_armor for equip in self.equipment_area)
    
    def get_equipped_armor(self) -> Optional[Card]:
        """获取已装备的防具"""
        for equip in self.equipment_area:
            if hasattr(equip, 'is_armor') and equip.is_armor:
                return equip
        return None
    
    def end_turn_reset(self):
        """回合结束时重置状态"""
        self.attack_count = 0
        self.extra_attack_count = 0
        self.has_drunk = False
    
    def __str__(self):
        return f"{self.name}({self.faction}) - 体力:{self.health}/{self.max_health} - 身份:{self.role}"